//... existing code ...
<template>
  <div class="login-container">
    <!-- 动态背景 -->
    <div class="tech-background">
      <div class="lines"></div>
      <div class="glow"></div>
      <div class="particles"></div>
    </div>

    <div class="login-box">
      <h2>智慧大棚监控系统</h2>
      <el-form @submit.prevent="handleLogin">
        <el-form-item>
          <el-input 
            v-model="username" 
            placeholder="用户名"
            :tabindex="1"
          >
            <template #prefix>
              <el-icon><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-input 
            v-model="password" 
            type="password" 
            placeholder="密码"
            :tabindex="2"
            @keyup.enter="handleLogin"
          >
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <div class="default-account">默认账号：root 密码：admin</div>
        <el-button 
          type="primary" 
          class="login-btn" 
          @click="handleLogin"
          :tabindex="3"
        >登录</el-button>
        <div class="register-link">
          没有账号？<router-link to="/register">去注册</router-link>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import { login } from '@/api'

const router = useRouter()
const username = ref('')
const password = ref('')

const handleLogin = async () => {
  if (!username.value || !password.value) {
    ElMessage.warning('请输入用户名和密码')
    return
  }

  try {
    console.log('正在尝试登录...')
    console.log('用户名:', username.value)
    
    const response = await login(username.value, password.value)
    console.log('登录响应:', response)
    
    if (!response) {
      throw new Error('服务器没有返回响应')
    }

    // 检查响应中是否直接包含 token
    if (response.token) {
      localStorage.setItem('token', response.token)
      console.log('Token已保存')
      ElMessage.success('登录成功')
      router.push('/dashboard')
    } 
    // 兼容旧格式
    else if (response.code === 200 && response.data && response.data.token) {
      localStorage.setItem('token', response.data.token)
      console.log('Token已保存')
      ElMessage.success('登录成功')
      router.push('/dashboard')
    } 
    else {
      console.error('登录失败，响应格式不正确:', response)
      ElMessage.error('登录失败，请联系管理员')
    }
  } catch (error) {
    console.error('登录失败，详细错误:', error)
    if (error.response) {
      console.error('错误响应:', error.response)
      console.error('错误状态:', error.response.status)
      console.error('错误数据:', error.response.data)
      
      if (error.response.status === 401) {
        ElMessage.error('用户名或密码错误')
      } else if (error.response.status === 500) {
        ElMessage.error('服务器内部错误，请稍后重试')
      } else {
        ElMessage.error(error.response.data?.message || '登录失败，请重试')
      }
    } else if (error.request) {
      console.error('没有收到响应:', error.request)
      ElMessage.error('无法连接到服务器，请检查网络连接')
    } else {
      console.error('请求配置错误:', error.message)
      ElMessage.error('登录请求失败，请重试')
    }
  }
}
</script>

//... existing code ...

<style scoped>
.login-container {
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #001529;
  position: relative;
  overflow: hidden;
}

/* 动态背景样式 */
.tech-background {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
  pointer-events: none;
}

.lines {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background:
      linear-gradient(90deg, rgba(0, 255, 255, 0.03) 1px, transparent 1px) 0 0,
      linear-gradient(0deg, rgba(0, 255, 255, 0.03) 1px, transparent 1px) 0 0;
  background-size: 50px 50px;
  animation: moveBg 20s linear infinite;
  pointer-events: none;
}

.glow {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 600px;
  height: 600px;
  background: radial-gradient(circle, rgba(0, 255, 255, 0.1) 0%, transparent 70%);
  transform: translate(-50%, -50%);
  animation: pulse 4s ease-in-out infinite;
  pointer-events: none;
}

.particles {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image:
      radial-gradient(rgba(0, 255, 255, 0.1) 1px, transparent 1px),
      radial-gradient(rgba(0, 255, 255, 0.15) 2px, transparent 2px);
  background-size: 40px 40px, 80px 80px;
  animation: float 20s linear infinite;
  pointer-events: none;
}

@keyframes moveBg {
  0% {
    transform: translate(0, 0);
  }
  100% {
    transform: translate(50px, 50px);
  }
}

@keyframes pulse {
  0% {
    opacity: 0.5;
    transform: translate(-50%, -50%) scale(0.8);
  }
  50% {
    opacity: 1;
    transform: translate(-50%, -50%) scale(1);
  }
  100% {
    opacity: 0.5;
    transform: translate(-50%, -50%) scale(0.8);
  }
}

@keyframes float {
  0% {
    background-position: 0 0, 0 0;
  }
  100% {
    background-position: 40px 40px, 80px 80px;
  }
}

.login-box {
  position: relative;
  z-index: 1;
  width: 400px;
  padding: 40px;
  background: rgba(0, 21, 41, 0.8);
  border-radius: 10px;
  box-shadow: 0 0 20px rgba(0, 255, 255, 0.2);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(0, 255, 255, 0.1);
}

.login-box::before {
  content: '';
  position: absolute;
  top: -2px;
  left: -2px;
  right: -2px;
  bottom: -2px;
  background: linear-gradient(45deg,
  rgba(0, 255, 255, 0.2),
  rgba(0, 255, 255, 0),
  rgba(0, 255, 255, 0.2)
  );
  border-radius: 12px;
  z-index: -1;
  animation: borderGlow 4s linear infinite;
  pointer-events: none;
}

@keyframes borderGlow {
  0% {
    filter: hue-rotate(0deg);
  }
  100% {
    filter: hue-rotate(360deg);
  }
}

h2 {
  color: #00ffff;
  text-align: center;
  margin-bottom: 30px;
  font-size: 24px;
  text-shadow: 0 0 10px rgba(0, 255, 255, 0.5);
}

.el-input {
  margin-bottom: 20px;
}

:deep(.el-input__wrapper) {
  background: rgba(0, 255, 255, 0.05);
  border: 1px solid rgba(0, 255, 255, 0.2);
  box-shadow: none !important;
}

:deep(.el-input__wrapper:hover) {
  border-color: rgba(0, 255, 255, 0.4);
}

:deep(.el-input__inner) {
  color: #fff;
  background: transparent;
  height: 40px;
  line-height: 40px;
}

:deep(.el-input__prefix-inner) {
  color: rgba(0, 255, 255, 0.6);
}

.login-btn {
  width: 100%;
  margin-top: 20px;
  background: linear-gradient(45deg, #00a8ff, #00fff2);
  border: none;
  height: 40px;
  font-size: 16px;
  transition: all 0.3s;
}

.login-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(0, 255, 255, 0.3);
}

.register-link {
  text-align: center;
  margin-top: 15px;
  color: rgba(255, 255, 255, 0.7);
}

.register-link a {
  color: #00ffff;
  text-decoration: none;
  margin-left: 5px;
}

.register-link a:hover {
  text-shadow: 0 0 5px rgba(0, 255, 255, 0.5);
}

.default-account {
  text-align: center;
  color: rgba(0, 255, 255, 0.6);
  margin-bottom: 10px;
  font-size: 14px;
}

:deep(.el-form-item) {
  margin-bottom: 20px;
}

:deep(.el-form) {
  display: block;
  width: 100%;
}
</style>